<?php
// +----------------------------------------------------------------------
// | 系统设置及PHP信息
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: ConfigAction.class.php 115 2011-05-11 09:06:41Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class ConfigAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
		
	//PHP信息
	public function phpInfo() {
		C('SHOW_RUN_TIME',false);
		C('SHOW_PAGE_TRACE',false);
		$this->display();
	}
	
	public function config() {
		$this->checkLimits();
		$vo = get_config();
		$cat  =  D('Category');
		$cat_option   = $cat->getCatList(0,$vo['random_posts_cat'] ,0,true);
		$this->assign('ur_here',L('config_'));
		$this->assign('cat_option', $cat_option);
		$this->assign('vo', $vo);
		$this->display();
	}
	
	public function saveConfig() {
		$this->checkLimits();
		if ($_REQUEST['conf']) {	
			$arr = array();
			$conf  =  M('Config');
			$data  = $conf->findAll();
			foreach ($data as $row) {
				$arr[$row['config_name']] = $row['config_value'];
			}
			foreach ($_FILES as $code => $file) {
				/* 判断用户是否选择了文件 */
				if ((isset($file['error']) && $file['error'] == 0) || (!isset($file['error']) && $file['tmp_name'] != 'none')) {
					if ($code == 'icp_file') {
						$path = ROOT_PATH ."cert/";			
						import("@.ORG.UploadFile");			
						$upload = new UploadFile();				
						$upload->maxSize    = return_bytes(@ini_get('upload_max_filesize'));
						$upload->allowExts  = array('cert');	
						$upload->savePath   = $path;	
						if (!$upload->upload()) {			
							$this->error($upload->getErrorMsg());				
						} else {
							$info =  $upload->getUploadFileInfo();					
							$conf->where("config_name = 'icp_file'")->setField('config_value',$info[0]["savepath"].$info[0]["savename"]);	
						}											
					}
					if ($code == 'watermark') {
						$path_ = ROOT_PATH . IMAGE_DIR;			
						import("@.ORG.UploadFile");			
						$upload = new UploadFile();				
						$upload->maxSize    = return_bytes(@ini_get('upload_max_filesize'));
						$upload->allowExts  = array('gif');	
						$upload->savePath   = $path_;	
						if (!$upload->upload()) {			
							$this->error($upload->getErrorMsg());				
						} else {
							$info =  $upload->getUploadFileInfo();					
							$conf->where("config_name = 'watermark'")->setField('config_value',$info[0]["savepath"].$info[0]["savename"]);	
						}											
					}
				}
			} 
			foreach ($_POST['config'] as $key => $val) {
				if ($arr[$key] != $val) {
					$conf->where("config_name = '$key'")->setField('config_value', trim($val));			
				}
			}
			/*echo "<pre>";
			print_r($_POST['config']);
			echo "</pre>";*/
			set_log('config(System)');
			clear_all_files();
			get_config($rewrite = true);
			$this->assign('jumpUrl',__URL__.'/config/');
			$this->success(L('success'));		
		} else {
			$this->error (L('illegal'));
		}
	}
	
	//发送测试邮件
	public function sendTestEmail() {
	
		if ($_POST['act']) {		
			$ms        = intval($_POST['mail_service']);
			$address   = trim($_POST['test_mail_address']);
			$smtp_host = trim($_POST['smtp_host']);
			$smtp_mail = trim($_POST['smtp_mail']);
			$smtp_port = trim($_POST['smtp_port']);
			$smtp_user = trim($_POST['smtp_user']);
			$smtp_pass = trim($_POST['smtp_pass']);
			$subject   = L('test_mail_title');
			$body      = L('email_content');
			
			if ($ms == 0 && function_exists('mail')) {
				$content_type = 'Content-Type: text/html; charset=utf-8';
				$headers = array();
				$headers[] = 'From: "' . '=?UTF8?B?' . base64_encode(C('blog_name')) . '?='.'" <' . $smtp_mail . '>';
				$headers[] = $content_type . '; format=flowed';	
				$res = @mail($address, '=?UTF8?B?' . base64_encode($subject) . '?=', $body, implode("\r\n", $headers));	
				if (!$res) {
					$this->error(L('email_error')); 
				} else {
					$this->success(L('email_success'));
				}
			} else {
				import('@.ORG.Phpmailer');				
				$mail = new PHPMailer();   
				$mail->IsSMTP();
				$mail->Username = $smtp_user; 
				$mail->Password = $smtp_pass;
				$mail->From     = $smtp_mail;   
				$mail->Host     = $smtp_host;
				$mail->Port     = $smtp_port;
				$mail->Mailer   = "smtp";   
				$mail->FromName = C('blog_name');  
				$mail->IsHTML(true); 
				$mail->SMTPAuth = true;
				$mail->CharSet  = "utf-8";
				$mail->Encoding = "base64";  
				$mail->Subject  = $subject; 
				$mail->AddAddress("$address", "");
				$mail->Body     = $body;
				if (!$mail->Send()) { 
					$this->error(L('email_error'));  
					continue;  
				} else {
					$this->success(L('email_success'));
				} 
				// Clear all addresses and attachments for next loop   
				$mail->ClearAddresses();   
				$mail->ClearAttachments();
			}
		} else {
			$this->error (L('illegal'));
		}
	}
	
	//删除ICP文件
	public function delIcpFile() {
		$this->checkLimits();
		@unlink(C('icp_file'));
		$conf  =  M('Config');
		$conf->where("config_name = 'icp_file'")->setField('config_value','');
		set_log('drop(File, Icp)');
		clear_all_files();
		$this->success(L('success'));
	}
	
	//删除水印文件
	public function delWatermark() {
		$this->checkLimits();
		@unlink(C('watermark'));
		$conf  =  M('Config');
		$conf->where("config_name = 'watermark'")->setField('config_value','');
		set_log('drop(File, Watermark)');
		clear_all_files();
		$this->success(L('success'));
	}
	
}

?>